<!--
 * @Date: 2024-05-29 21:17:38
 * @LastEditors: Penn.ji
 * @LastEditTime: 2024-05-30 21:40:55
 * @FilePath: \chrome-tools\mini-toolbox-gitee\src\views\models\search-image\index.vue
-->
<template>
  <div class="search-image">
    <el-input
      v-model="searchVal"
      placeholder="请输入关键字"
      class="input-with-select"
      @keyup.enter="toQuery(true)"
    >
      <template #append>
        <el-button @click.stop="toQuery(true)">搜索</el-button>
      </template>
    </el-input>
    <ul v-infinite-scroll="toQuery"
      :infinite-scroll-distance="10"
      :infinite-scroll-immediate="false"
      class="infinite-list" style="overflow: auto">
      <li v-for="(item, i) in data" :key="`img_${i}`" class="infinite-list-item">
        <el-image :src="item.thumbnailUrl" fit="fill">
            <template #error>
              <div class="image-slot">
                <p>loading</p>
              </div>
            </template>
        </el-image>
      </li>
    </ul>
  </div>
</template>
<script lang="ts">
import { ref, computed } from "vue";
import { searchImage } from "@/api";
import { ElMessage } from "element-plus";
export default {
  name: "SearchImage",
  setup() {
    const searchVal = ref("");
    let pageNumber = 1;
    const data = ref<any>([]);
    const toQuery = (reload: boolean) => {
      if(!searchVal.value) {
        return
      }
      if(reload) {
        console.log('-- reload');
        pageNumber = 1
        data.value = []
      } else {
        pageNumber++
      }
      searchImage(searchVal.value, pageNumber).then(res => {
        data.value = res?.data?.data || []
      }).catch(c => {
        console.log('Error ' + c);
      })
    };
    return { data, toQuery, searchVal };
  },
};
</script>

<style lang="scss">
.search-image {
  .infinite-list {
    height: 300px;
    padding: 0;
    margin: 0;
    list-style: none;
  }
  .infinite-list .infinite-list-item {
    display: flex;
    align-items: center;
    justify-content: center;
    // height: 50px;
    margin: 10px;
    color: var(--el-color-primary);
  }
  .infinite-list .infinite-list-item + .list-item {
    margin-top: 10px;
  }
}
</style>
